<!-- 当前库存量 -->
<template>
  <div class="app-container">
    <el-drawer v-model="MyDrawer" :direction="'ttb'" size="small" :with-header="false">
      <span @click="MyDrawer = false" style="float: right; cursor: pointer; color: rgb(7, 7, 7)">X</span>
      <Find :searchImg="false" :formList="formList14" @findClicked="findClicked14" />
      <h1 class="f16">当前库存量</h1>
      <Table :tableHeader="tableHeader14" :tableData="tableData14" @download="download14" />
    </el-drawer>
  </div>
</template>

<script lang="ts" setup name="KouJian">
import JMessage from '@/utils/JMessage'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted } from 'vue'
import { watch } from 'vue'
import { ref } from 'vue'

const MyDrawer = ref(false)
watch(MyDrawer, (newVal) => {
  if (!newVal) {
    emits('propsDrawerFalse')
  }
})
const props = defineProps({
  parentCurrentRow: {
    type: Object,
    default: () => {}
  },
  drawer: {
    type: Boolean,
    default: false
  }
})
const emits = defineEmits(['success', 'propsDrawerFalse'])
watch(
  () => props.drawer,
  (newVal) => {
    MyDrawer.value = newVal
    if (newVal) {
      getTableList14()
      formList14.value[0].inputValue = props.parentCurrentRow.mtlNo
      formList14.value[2].inputValue = props.parentCurrentRow.qty
    }
  }
)
// 获取当前库存量
const formList14 = ref([
  {
    type: 'input',
    inputPlaceholder: '',
    inputValue: '',
    inputLabel: '物料编码'
  },
  {
    type: 'select',
    selectPlaceholder: '请选择仓库',
    selectValue: '',
    selectLabel: '仓库',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '',
    inputValue: '',
    inputLabel: '请购量'
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  }
]) as any
const findClicked14 = async (content: string) => {
  if (content === '置空') {
    formList14.value.forEach((item: any) => {
      ;(item.inputValue = ''), (item.selectValue = '')
    })
  }
  if (content === '查询') {
    getTableList14()
  }
}
const tableHeader14 = ref([
  {
    change: true,
    label: '所属仓库ID',
    prop: 'stkId'
  },
  {
    change: true,
    label: '仓库名称',
    prop: 'warehouseName'
  },
  {
    change: true,
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    change: true,
    label: '库存量',
    prop: 'stkQty'
  },
  {
    label: '扣减量',
    prop: 'reduceQty'
  },
  {
    type: 'function',
    label: '操作列',
    prop: [{ label: '确认' }]
  }
])
const tableData14 = ref([])
const isLoading14 = ref(true)
const currentTableRow14 = ref({}) as any
const currentRow14 = async (row: any = tableData14.value[0]) => {
  currentTableRow14.value = row
}
const getTableList14 = debounce(async () => {
  isLoading14.value = true
  let tableDataList14 = await service.post<any, any>(`/store/dev/deduct/queryInv`, [
    {
      lotNo: props.parentCurrentRow.orderId,
      mtlId: formList14.value[0].inputValue,
      stkId: formList14.value[1].selectValue.split('  ')[0],
      warehouseName: formList14.value[1].selectValue.split('  ')[1],
      address: formList14.value[1].selectValue.split('  ')[2]
    }
  ])
  isLoading14.value = false
  tableData14.value = tableDataList14.data.map((item: any) => {
    item.reduceQty = item.stkQty
    item.change = 'Y'
    item.disableConfirm = true
    item.mtlNo = item.mtlBase.mtlNo
    return item
  })
  currentRow14()
}, 500)
const download14 = async (row: any) => {
  if (row.reduceQty > row.stkQty) {
    JMessage.error('扣减库存不能大于库存')
    return
  }
  const res = await service.post<any, any>(
    `/mes/kf/material/reduceInventory?id=${props.parentCurrentRow.id}&stkId=${formList14.value[1].selectValue.split('  ')[0]}&stkQty=${row.reduceQty}`
  )
  if (res.code === 200) {
    JMessage.success('扣减库存成功')
    getTableList14()
    emits('success')
  } else {
    JMessage.error(res.msg)
  }
}
watch(
  () =>
    formList14.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList14()
  }
)

const getSelectOptions = async () => {
  // 获取仓库下拉框
  const res = (await service.get(`/store/warehouse/detail/back?search=${''}`)) as any
  formList14.value[1].selectOptions = res.data.map((item: any) => ({
    optionKey: item.stkId,
    optionValue: `${item.stkId}  ${item.warehouseName}  ${item.address}`,
    LeftLabel: item.warehouseName,
    RightLabel: item.address,
    optionLabel: `${item.warehouseName}  ${item.address}`
  }))
  formList14.value[1].selectValue = `${res.data[0].stkId}  ${res.data[0].warehouseName}  ${res.data[0].address}`
}
onMounted(() => {
  getSelectOptions()
})
</script>
